package com.zjxf.utils;

import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.SysConst;
import com.zjxf.token.MyUserNamePasswordToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * 登录 工具类
 *
 * @Author fhx
 * @Date 2020/6/5 15:38
 */
@Slf4j
public class LoginUtil {

    /**
     * 退出成功
     *
     * @return JSONResponse
     */
    public static JSONResponse loginOut() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return JSONResponse.success("退出成功");
    }

    /**
     * 登录工具类型
     *
     * @param
     * @return JSONResponse
     */
    public static JSONResponse login(String username,String password,Boolean remeberMe,Integer type) {
        SysConst.loginType loginTypeEnum = SysConst.getLoginType(type).get();
        log.info("用户" + username + "请求登录:" + loginTypeEnum.getName());
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout();
        }
        MyUserNamePasswordToken token =
                new MyUserNamePasswordToken(username, password, remeberMe, type);
        subject.login(token);
        log.info("认证成功: ---认证信息:{}", username);
        return JSONResponse.success("登录成功");
    }


}
